



function  getCommodityDetail() {
    let itemId = _common_util.getURLParam('itemId');

    $.ajax({
        type : 'GET',
        url  : _common_util.getServerURL('commodity/item_detail')+'?itemId='+itemId ,
        async: false,
        success : function(res){
             console.log(res);

            $('#categoryId').html(res.data.categoryId);
            $('#itemName').html(res.data.itemName);
            $('#price').html('&#36;'+res.data.price);
            $('#description').html(res.data.description);
            $('#itemId').html('<strong>编号</strong>'+res.data.itemId);


            let catestr = ' <a href="home.html">Home</a>\n' +
                '                    <span class="delimiter"><i class="fa fa-angle-right"></i></span>\n' +
                '                    <a href="shop-fw.html?category='+res.data.categoryId+'" >'+res.data.categoryName+'</a>\n' +
                '                    <span class="delimiter"><i class="fa fa-angle-right"></i>\n' +
                '                        </span> <span >'+res.data.itemName+'</span>'

            $('.woocommerce-breadcrumb').append(catestr);



            itemId = res.data.itemId;
            let imgsrc= _common_util.getImageUrl(res.data.image);
            $('#gallery-big-list').append('<a href="'+imgsrc+'" class="zoom" title="" data-rel="prettyPhoto[product-gallery]"><img src="'+imgsrc+'" data-echo="'+imgsrc+'" class="wp-post-image"  style="width: 470px;height: 470px" alt=""></a>');
            $('#gallery-sm-list').append('<a href="'+imgsrc+'" class="zoom" title="" data-rel="prettyPhoto[product-gallery]"><img src="'+imgsrc+'" data-echo="'+imgsrc+'" class="wp-post-image" style="height: 87px"  alt=""></a>');

            $.each(res.data.subImages,function (i,subimg) {
                let subimgsrc= _common_util.getImageUrl(subimg);

                $('#gallery-big-list').append('<a href="'+subimgsrc+'" class="zoom" title="" data-rel="prettyPhoto[product-gallery]"><img src="'+subimgsrc+'" data-echo="'+subimgsrc+'" class="wp-post-image"  style="width: 600px;height: 470px" alt=""></a>');
                $('#gallery-sm-list').append('<a href="'+subimgsrc+'" class="zoom" title="" data-rel="prettyPhoto[product-gallery]"><img src="'+subimgsrc+'" data-echo="'+subimgsrc+'" class="wp-post-image" style="height: 87px" alt=""></a>');

            })
            let pec =res.data.star*2 * 10 +'%';
            $("#product-star-pec").css('width',pec);
            $('#product-name-count').html(res.data.star);
            $('#avgstar').html(res.data.star);
            if (res.data.stock!= 0){
                $('#stock').html('库存：('+res.data.stock+')');
                $('.in-stock').css('display','inline-block');
            }else {
                $('.out-of-stock').css('display','inline-block');
            }
        }

    })
}


function getComment(itemId,pageSize,pageNum) {

    $.ajax({
        type        :       'GET',
        url         :       _common_util.getServerURL('commodity/item_comment'),
        dataType    :       'json',
        data        :       {
            'itemId'    :   itemId,
            'pageSize'  :   pageSize,
            'pageNum'   :   pageNum
        },
        success:function (res) {
            console.log(res);
            if (res.status === 0) {
                let total;
                let star = [0,0,0,0,0,0];
                $.each(res.data.commodityRateVOList,function (i,item) {
                    let starrate = ((item.itemStar*1)/5)*100 +'%';
                    star[item.itemStar] += 1;
                    star[0] += 1 ;
                    let commentstr = '                                                    <li class="comment odd alt thread-odd thread-alt depth-1">\n' +
                        '\n' +
                        '                                                        <div class="comment_container">\n' +
                        '\n' +
                        '                                                            <img alt=\'\' src="assets/images/blog/avatar.jpg" class="avatar" height=\'60\' width=\'60\' />\n' +
                        '                                                            <div class="comment-text">\n' +
                        '\n' +
                        '                                                                <div itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating" class="star-rating" title="Rated 5 out of 5">\n' +
                        '                                                                    <span style="width:'+starrate+'"><strong itemprop="ratingValue">'+item.itemStar+'</strong> out of 5</span>\n' +
                        '                                                                </div>\n' +
                        '\n' +
                        '                                                                <p class="meta">\n' +
                        '                                                                    <strong>'+item.username+'</strong> &ndash;\n' +
                        '                                                                    <time itemprop="datePublished" datetime="'+item.createTime+'">'+item.createTime+'</time>:\n' +
                        '                                                                </p>\n' +
                        '\n' +
                        '\n' +
                        '                                                                <div itemprop="description" class="description">\n' +
                        '                                                                    <p> '+item.comment+' </p>\n' +
                        '                                                                </div>\n' +
                        '\n' +
                        '                                                                <p class="meta">\n' +
                        '                                                                    <strong itemprop="author">'+item.username+'</strong> &ndash; <time itemprop="datePublished" datetime="2016-03-03T14:14:47+00:00">'+item.createTime+'</time>\n' +
                        '                                                                </p>\n' +
                        '\n' +
                        '                                                            </div>\n' +
                        '                                                        </div>\n' +
                        '                                                    </li><!-- #comment-## -->\n';

                        $('#comment-list').append(commentstr);

                })
                console.log(star);
                for (let i =1;i<=5;i++){
                    total += i * star[i];
                    let  pec ;
                    if (star[i]===0){
                        pec = '0%'
                    }else {
                        pec = ( star[i]/star[0] )*100 + '%';
                    }
                    console.log(pec);
                    $('#star'+i).css('width',pec);
                    $('#star'+i+'count').html(star[i]);
                }


                totalpage = res.data.endRow;
                currentpage = res.data.pageNum;
                prepage = res.data.prePage;
                nextpage = res.data.nextPage;
                restpage = totalpage - currentpage;
                console.log("剩余的页数："+restpage);
                let pagestr='';
                $('#page-numbers').html('');

                if (currentpage>3){
                    //1和省略号
                    pagestr = pagestr+'               <li><a onclick="toPage(this)" page="1" class="page-numbers">1</a></li> \n' +
                        '                              <li class="apostrophe">...</li>' +
                        '                             <li><a onclick="toPage(this)" page="'+prepage+'" class="page-numbers">'+prepage+'</a></li>';
                }else {
                    for (var i = 1; i<currentpage;i++) {
                        pagestr = pagestr + '                <li><a onclick="toPage(this)" page="' + i + '" class="page-numbers">' + i + '</a></li>';
                    }
                }
                pagestr = pagestr + '                   <li><a onclick="toPage(this)" page="'+currentpage+'" class="page-numbers current">'+currentpage+'</a></li>\n';
                if (restpage>2){
                    pagestr = pagestr + '               <li><a onclick="toPage(this)" page="'+nextpage+'" class="page-numbers">'+nextpage+'</a></li>       ' +
                        '               <li class="apostrophe">...</li>\n' +
                        '               <li><a onclick="toPage(this)" page="'+totalpage+'" class="page-numbers">'+totalpage+'</a></li>';
                }else if (nextpage != totalpage && res.data.hasNextPage) {

                    pagestr = pagestr + '               <li><a onclick="toPage(this)" page="'+nextpage+'" class="page-numbers">'+nextpage+'</a></li> ' +
                        '               <li><a onclick="toPage(this)" page="'+totalpage+'" class="page-numbers">'+totalpage+'</a></li> ';
                }else if (nextpage === totalpage){
                    pagestr = pagestr + ' <li><a onclick="toPage(this)" page="'+totalpage+'" class="page-numbers">'+totalpage+'</a></li>';
                }
                $('#page-numbers').html(pagestr);


            }
        }
    })
}




function single_add_to_cart(){
    let itemId = _common_util.getURLParam('itemId');
    let quantity = $('#quantityToCart').val();

    $.ajax({
        type: 'POST',
        url:  _common_util.getServerURL('cart/add_item'),
        dataType:'json',
        data:{
            'itemId' : itemId,
            'quantity' : quantity
        },
        success:function (res) {
            if (res.status === 0){
                window.wxc.xcConfirm('添加至购物车成功','success')
            }else if (res.status === 3){
                window.wxc.xcConfirm('尚未登录，请先登录','info');
            }else if (res.status === 2){
                console.log('数量错误');
                $.ajax({
                    type : 'GET',
                    url  : _common_util.getServerURL('commodity/item_detail')+'?itemid='+itemId ,
                    success : function(res){
                        let stock = res.data.stock;
                        if (quantity > stock){
                            $('#quantity-error').html('数量超过库存，现库存为'+stock+'，请重新加入');
                            $('#quantityToCart').val(stock);
                        }
                        if (quantity <res.data.stock){
                            $('#quantity-error').html('');
                        }
                    }
                })
            }


        }
    })
}


function purchase_item(itemId){

    $.ajax({
        type:'GET',
        url: _common_util.getServerURL('commodity/purchase_item'),
        dataType: 'json',
        data: {
            'itemId': itemId
        },
        success:function (res) {
            if (res.status===0){
                window.location.href='./single-order.html?itemId='+itemId;
            }else if (res.status===3){
                window.wxc.xcConfirm('尚未登录，请先登录','info');
            }
        }
    })
}


function toPage(item){
    let page =$(item).attr('page');
    let keyword =$('#input-search').val();
    console.log('cccccpage:'+page);
    let itemId = _common_util.getURLParam('itemId');
    getComment(itemId,'',page);
}




$(function () {
    let itemId = _common_util.getURLParam('itemId')
    getCommodityDetail();

    $('#single_add_to_cart_button').on('click',function () {
        console.log('btn click');
        single_add_to_cart();
    });

    $('#buy-btn').on('click',function () {
        console.log('buy-btn click');
        let item = _common_util.getURLParam('itemId');
        purchase_item(item);
    })



    getComment(itemId,'','');


})




